package com.yc.ui.dialog;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.ui.MainUI;
import com.yc.utils.MailUtils;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MoneyDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private String c_cardid;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public MoneyDialog(Shell parent, int style, String c_cardid) {
		super(parent, style);
		setText("SWT Dialog");
		this.c_cardid = c_cardid;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		
		shell = new Shell(getParent(), getStyle());
		shell.setSize(410, 213);
		shell.setText(getText());
		//界面居中显示
		SWTUtil.centerWindow(shell);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(72, 45, 76, 20);
		lblNewLabel.setText("充值金额：");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(169, 42, 106, 26);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String num = text.getText().trim();
				
				if(!MailUtils.isNumber(num)) {
					text.setText("1");
					return ;
				}
				double money = 0;
				if(MailUtils.isNumber(num)) {
					money = Double.parseDouble(num);
				}else {
					return ;
				}
				LoginIDao dao = new LoginDao();
				int re = 0;
				re = dao.ChargeMone(c_cardid, money);
				if(re == 0) {
					StringUtil.showMessageBox(getParent(), "充值失败");
				}else {
					StringUtil.showMessageBox(getParent(), "充值成功");
					money = Double.parseDouble(dao.GetMoney(c_cardid).get("c_money").toString());
					shell.dispose();
					MainUI.getMoney();
				}
				
			}
		});
		btnNewButton.setBounds(93, 108, 98, 30);
		btnNewButton.setText("充值");
		
		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnNewButton_1.setBounds(223, 108, 98, 30);
		btnNewButton_1.setText("取消");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(285, 45, 41, 20);
		label.setText("元");

	}

}
